package com.yanqu.road.dao.impl.channelcenter.tonbing;

import com.yanqu.road.dao.cache.SqlCacheParam;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempNoPoolCacheDao;
import com.yanqu.road.entity.activity.tonbingchannel.config.*;
import com.yanqu.road.entity.activity.washe.config.WasheBoxConfig;
import com.yanqu.road.entity.activity.washe.config.WasheConfig;
import com.yanqu.road.entity.enums.washe.eWasheFunction;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

public class TempChannelTonBingConfigDaoImpl extends TempNoPoolCacheDao {

    public TempChannelTonBingConfigDaoImpl(String dbParam) {
        super(dbParam);
    }

    private static String getTonBingBuffTypeConfigSql = "select * from `t_n_activity_peaktongbing_buff` where `activityId`=?";

    public Map<Integer, ChannelTonBingBuffTypeConfig> getTonBingBuffTypeConfigMap(int activityId) {
        SqlCacheParam sqlCacheParam = getCacheParam(getTonBingBuffTypeConfigSql);
        sqlCacheParam.addParam(Integer.class, activityId);
        // 先取缓存
        Object cacheResult = getCacheResult(sqlCacheParam);
        if (cacheResult != null) {
            return (Map<Integer, ChannelTonBingBuffTypeConfig>) cacheResult;
        }
        Map<Integer, ChannelTonBingBuffTypeConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            call = createCall(sqlCacheParam);
            rs = call.executeQuery();
            while (rs.next()) {
                ChannelTonBingBuffTypeConfig data = new ChannelTonBingBuffTypeConfig();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setType(rs.getInt("type"));
                data.setIcon(rs.getInt("icon"));
                data.setName(rs.getString("name"));
                data.setDesc(rs.getString("desc"));
                data.setBuffEffect(rs.getString("buffEffect"));
                data.setContinueTime(DateHelper.HOUR_MILLIONS * rs.getInt("continueTime"));
                map.put(data.getId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            closeConnection(call, rs);
        }
        return map;
    }

    private static String getTonBingCityConfigSql = "select * from `t_n_activity_peaktongbing_city` where `activityId`=?";

    public Map<Integer,Map<Integer, ChannelTonBingCityConfig>> getTonBingCityConfigMap(int activityId) {
        SqlCacheParam sqlCacheParam = getCacheParam(getTonBingCityConfigSql);
        sqlCacheParam.addParam(Integer.class, activityId);
        // 先取缓存
        Object cacheResult = getCacheResult(sqlCacheParam);
        if (cacheResult != null) {
            return (Map<Integer,Map<Integer, ChannelTonBingCityConfig>>) cacheResult;
        }
        Map<Integer,Map<Integer, ChannelTonBingCityConfig>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            call = createCall(sqlCacheParam);
            rs = call.executeQuery();
            while (rs.next()) {
                ChannelTonBingCityConfig data = new ChannelTonBingCityConfig();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setName(rs.getString("name"));
                data.setMapType(rs.getInt("mapType"));
                data.setType(rs.getInt("type"));
                data.setInitialCamp(rs.getInt("initialCamp"));
                data.setShowType(rs.getInt("showType"));

                String specialAddition = rs.getString("specialAddition");
                data.setSpecialAddition(specialAddition);
                Map<Integer, Integer> specAddiMap = new ConcurrentHashMap<>();
                if (!Objects.equals("0", specialAddition)) {
                    List<Integer> list = StringUtils.stringToIntegerList(specialAddition, ";");
                    specAddiMap.put(list.get(0), list.get(1));
                }
                data.setSpecialAdditionMap(specAddiMap);
                data.setWarType(rs.getInt("warType"));
                if(!map.containsKey(data.getWarType())){
                    map.put(data.getWarType(),new ConcurrentHashMap<>());
                }
                map.get(data.getWarType()).put(data.getId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            closeConnection(call, rs);
        }
        return map;
    }

    private static String getTonBingCityTypeConfigSql = "select * from `t_n_activity_peaktongbing_type` where `activityId`=?";

    public Map<Integer,Map<Integer, ChannelTonBingCityTypeConfig>> getTonBingCityTypeConfigMap(int activityId) {
        SqlCacheParam sqlCacheParam = getCacheParam(getTonBingCityTypeConfigSql);
        sqlCacheParam.addParam(Integer.class, activityId);
        // 先取缓存
        Object cacheResult = getCacheResult(sqlCacheParam);
        if (cacheResult != null) {
            return (Map<Integer,Map<Integer, ChannelTonBingCityTypeConfig>>) cacheResult;
        }
        Map<Integer,Map<Integer, ChannelTonBingCityTypeConfig>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            call = createCall(sqlCacheParam);
            rs = call.executeQuery();
            while (rs.next()) {
                ChannelTonBingCityTypeConfig data = new ChannelTonBingCityTypeConfig();
                data.setActivityId(rs.getInt("activityId"));
                data.setType(rs.getInt("type"));
                data.setWarType(rs.getInt("warType"));
                data.setIcon(rs.getInt("icon"));
                data.setName(rs.getString("name"));
                data.setFoodProduction(rs.getInt("foodProduction"));
                data.setTeamNum(rs.getString("teamNum"));
                data.setSoldierNum(rs.getString("soldierNum"));
                data.setAddition(rs.getInt("addition"));
                data.setBuffEffect(Integer.valueOf(rs.getString("buffEffect")));
                data.setCampPoint(rs.getString("campPoint"));
                data.setExtraTeamNum(rs.getInt("extraTeamNum"));
                if(!map.containsKey(data.getWarType())){
                    map.put(data.getWarType(),new ConcurrentHashMap<>());
                }
                map.get(data.getWarType()).put(data.getType(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            closeConnection(call, rs);
        }
        return map;
    }
    private static String getTonBingNpcEventConfigSql = "select * from `t_n_activity_peaktongbing_event` where `activityId`=?";

    public Map<Integer, ChannelTonBingNpcEventConfig> getTonBingNpcEventConfigMap(int activityId) {
        SqlCacheParam sqlCacheParam = getCacheParam(getTonBingNpcEventConfigSql);
        sqlCacheParam.addParam(Integer.class, activityId);
        // 先取缓存
        Object cacheResult = getCacheResult(sqlCacheParam);
        if (cacheResult != null) {
            return (Map<Integer, ChannelTonBingNpcEventConfig>) cacheResult;
        }
        Map<Integer, ChannelTonBingNpcEventConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            call = createCall(sqlCacheParam);
            rs = call.executeQuery();
            while (rs.next()) {
                ChannelTonBingNpcEventConfig data = new ChannelTonBingNpcEventConfig();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setType(rs.getInt("type"));
                data.setWeight(rs.getInt("weight"));
                data.setIcon(rs.getString("icon"));
                data.setRewards(rs.getString("rewards"));
                // 缓存
                data.setRewardList(StringUtils.stringToStringList(data.getRewards(), "\\|"));
                map.put(data.getId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            closeConnection(call, rs);
        }
        return map;
    }


    private static String getTonBingTitleConfigSql = "select * from `t_n_activity_peaktongbing_title` where `activityId`=?";

    public Map<Integer, Map<Integer, ChannelTonBingTitleConfig>>  getTonBingTitleConfigMap(int activityId) {
        SqlCacheParam sqlCacheParam = getCacheParam(getTonBingTitleConfigSql);
        sqlCacheParam.addParam(Integer.class, activityId);
        // 先取缓存
        Object cacheResult = getCacheResult(sqlCacheParam);
        if (cacheResult != null) {
            return (Map<Integer, Map<Integer, ChannelTonBingTitleConfig>>) cacheResult;
        }
        Map<Integer, Map<Integer, ChannelTonBingTitleConfig>>  map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            call = createCall(sqlCacheParam);
            rs = call.executeQuery();
            while (rs.next()) {
                ChannelTonBingTitleConfig data = new ChannelTonBingTitleConfig();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setType(rs.getInt("type"));
                data.setIcon(rs.getInt("icon"));
                data.setName(rs.getString("name"));
                data.setEffect(rs.getInt("effect"));
                data.setRewards(rs.getString("rewards"));
                data.setCampId(rs.getInt("campId"));
                if(!map.containsKey(data.getCampId())){
                    map.put(data.getCampId(),new ConcurrentHashMap<>());
                }
                map.get(data.getCampId()).put(data.getId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            closeConnection(call, rs);
        }
        return map;
    }
}
